from flask import Flask, render_template, Response, make_response, jsonify, url_for, redirect, request

app = Flask(__name__)


@app.route('/hello')
def hello_world():  # put application's code here
    return 'Hello World!'
@app.route('/login')
def login():  # put application's code here
    return render_template('login.html')
@app.route('/dologin',methods=['POST'])
def dologin():  # put application's code here
    result = request.form
    print(result)



@app.route('/main/<num>', methods=['GET', 'POST'])
def main(num):  # put application's code here
    return render_template('index.html', num = num)
@app.route('/index')
def index():
    # 使用Response类的构造方法生成响应对象，设置状态码为201，响应类型为text/html
    # resp = Response(response='Python&Flask',status=201,
    #                              content_type='text/html;charset=utf-8')

    #通过make_response()函数生成JSON类型的响应数据
    user1 = {'name':'John', 'age': 20}
    user2 = {'name':'Mike', 'age': 19}
    list_user = [user1, user2]
    # resp = make_response(jsonify({'Python': 'Flask'}), 202)
    resp = make_response(jsonify(list_user), 202)
    return resp
@app.route('/hello/flask/<name>')
def greet(name):
    return f"{url_for('greet',name = name,age = 20)}"   # 反向解析视图函数greet()对应的URL
@app.route('/hello/python')
def great():
    return redirect(url_for("hello_world"))


if __name__ == '__main__':
    app.run()